
package com.compuvest.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Customer implements java.io.Serializable {

    public static final Logger log = LoggerFactory.getLogger(Customer.class);

    private int customerID = 0;
    private String type = "";
    private String email = "";
    private String password = "";
    private String resellerCode = "";
    private String accwareAccount = "";
    private boolean useShippingAccount = false;
    private String shippingAccount = "";

    public Address shipping = null;
    public Address billing = null;

    private String ccName = "";
    private String cardNumber = "";
    private int ccNumberAdd = 0;
    private String ccExpMonth = "";
    private String ccExpYears = "";
    private String bankPhone = "";

    private String notice = "";
    private String joined = "";
    private String hear = "";

    public Customer() {
	shipping = new Address();
	billing = new Address();
    }

    public int getCustomerID()                          { return customerID; }
    public void setCustomerID(int customerID)           { this.customerID = customerID; }

    public String getType()                             { return type; }
    public void setType(String type)                    { this.type = type; }

    public String getResellerCode()                     { return resellerCode; }
    public boolean hasResellerCode()                    { return resellerCode.length() != 0; }
    public void setResellerCode(String resellerCode)    { this.resellerCode = defaultValue(resellerCode); }

    public boolean isUseShippingAccount()		{ return useShippingAccount; }
    public void setUseShippingAccount(boolean useShippingAccount){ this.useShippingAccount = useShippingAccount; }

    public String getShippingAccount()                  { return shippingAccount; }
    public void setShippingAccount(String shippingAccount){ this.shippingAccount = shippingAccount; }

    public String getAccwareAccount()                   { return accwareAccount; }
    public void setAccwareAccount(String accwareAccount){ this.accwareAccount = defaultValue(accwareAccount); }

    public String getEmail()                            { return email; }
    public void setEmail(String email)                  { this.email = defaultValue(email); }

    public String getPassword()                         { return password; }
    public boolean hasPassword()                        { return password.length() != 0; }
    public void setPassword(String password)            { this.password = defaultValue(password); }


    public Address getShippingAddress()			{ return shipping; }
    public void setShippingAddress(Address shipping)	{ this.shipping = shipping; }

    public Address getBillingAddress()			{ return billing; }
    public void setBillingAddress(Address billing)	{ this.billing = billing; }


    public String getCName()				{ return ccName; }
    public void setCName(String ccName)			{ this.ccName = defaultValue(ccName); }

    // recognaze Card type from first digit
    public String getCType() {
	String cardType = "";
        if(cardNumber.length() > 1) {

            char first = cardNumber.charAt(0);
            switch(first) {
                case '3' : cardType = "AMEX"; break;
                case '4' : cardType = "VISA"; break;
                case '5' : cardType = "MASTERCARD"; break;
                case '6' : cardType = "DISCOVER"; break;
                default : cardType = "";
            }
        }
        return cardType;
    }
//    public void setCType(String ccType)			{ this.ccType = ccType; }

    public String getCardNumber()			{ return cardNumber; }
    public void setCardNumber(String cardNumber)	{ this.cardNumber = cardNumber; }


    // change number card ftom 4111 1111 1111 1111 to **** **** **** 1111
    public String getCardNumberShow() {
        String show;
        try {
            if("0".equals(this.cardNumber) || this.cardNumber.isEmpty()) { return ""; }

            show = new StringBuilder(16).append(this.cardNumber).delete(4,16).insert(4,"************").toString();
        }
        catch(Exception ex) {
            log.error("getCardNumberShow:this.cardNumber=" + this.cardNumber + "\n" + ex);
            show = "";
        }
	return show;
    }

    public int getCNumberAdd()				{ return ccNumberAdd; }
    public void setCNumberAdd(int ccNumberAdd)		{ this.ccNumberAdd = ccNumberAdd; }

    public String getCExpDate()				{ return ccExpMonth + ccExpYears; }

    public String getCExpMonth()			{ return ccExpMonth; }
    public void setCExpMonth(String ccExpMonth)		{ this.ccExpMonth = ccExpMonth; }

    public String getCExpYears()			{ return ccExpYears; }
    public void setCExpYears(String ccExpYears)		{ this.ccExpYears = ccExpYears; }

    public String getBankPhone()			{ return bankPhone; }
    public void setBankPhone(String bankPhone)		{ this.bankPhone = defaultValue(bankPhone); }


    public String getNotice() { return notice.length() > 250 ? notice.substring(0, 250) : notice; }
    public void setNotice(String notice)		{ this.notice = defaultValue(notice); }

    public String getHearAboutUs()			{ return hear; }
    public void setHearAboutUs(String hear)		{ this.hear = defaultValue(hear); }

    public String getJoined()				{ return joined; }
    public void setJoined(String joined)		{ this.joined = joined; }

    private String defaultValue(String str) {
	return str == null ? "" : str.trim();
    }

    // check shipping == billing
    public boolean compareBillingAndShipping() {
	String b = new StringBuilder(200).append(billing.getCountry()).append(billing.getState()).append(billing.getZip())
			.append(billing.getCity()).append(billing.getAddress2()).append(billing.getAddress1()).toString();
	String s = new StringBuilder(200).append(shipping.getCountry()).append(shipping.getState()).append(shipping.getZip())
			.append(shipping.getCity()).append(shipping.getAddress2()).append(shipping.getAddress1()).toString();
	return b.equalsIgnoreCase(s);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        sb.append("\ncustomerID:           ").append(customerID);
        sb.append("\ntype:                 ").append(type);
        sb.append("\nresellerCode:         ").append(resellerCode);
        sb.append("\nisUseShippingAccount: ").append(useShippingAccount);
        sb.append("\nshippingAccount:      ").append(shippingAccount);
        sb.append("\naccwareAccount:       ").append(accwareAccount);
        sb.append("\nemail:                ").append(email);
        sb.append("\npassword:             ").append(password);
        sb.append("\n----shipping----");
        sb.append(shipping.toString());
        sb.append("\n----billing----");
        sb.append(billing.toString());
        sb.append("\nccName:              ").append(ccName);
        sb.append("\ncardNumber:          ").append(cardNumber);
        sb.append("\ncardNumberShow:      ").append(getCardNumberShow());
        sb.append("\nccNumberAdd:         ").append(ccNumberAdd);
        sb.append("\nccExpDate:           ").append(ccExpMonth).append(ccExpYears);
        sb.append("\nccExpMonth:          ").append(ccExpMonth);
        sb.append("\nccExpYears:          ").append(ccExpYears);
        sb.append("\nbankPhone:           ").append(bankPhone);
        sb.append("\nnotice:              ").append(getNotice());
        sb.append("\nhearAboutUs:         ").append(hear);
        sb.append("\njoined:              ").append(joined);
        sb.append("\ncompare:             ").append(compareBillingAndShipping());

        return sb.toString();
    }

}
